package handlers

// 作废
import (
	"cron-go/configs/error_code"
	"cron-go/internal/pkg/crontab/dao"
	"cron-go/internal/pkg/crontab/models"
	"cron-go/internal/pkg/crontab/task"
	"fmt"

	"github.com/go-liam/util/conv"
	"github.com/go-liam/util/response"
)

type RunTask struct {
	Req   *RunTaskReq
	Model *models.TaskMysqlModel
	IDs   []int64
}

type RunTaskReq struct {
	IDs []string `json:"ids" validate:"required" ` // crontab
}

func (s *RunTask) GetData() *response.APIResponse {
	if !s.CheckData() {
		return &response.APIResponse{Code: error_code.RequestParameter, Message: error_code.MsRequest, Data: ""}
	}
	s.IDs = conv.ArrayStringToInt64(s.Req.IDs)
	//get list
	page := &response.Pagination{PageSize: 50000, Current: 1}
	where := fmt.Sprintf(" id in (%v) ", conv.ArrayToString(s.IDs, ","))
	r := &response.ListParameter{WhereSt: where, OrderSt: " order by id "}
	ls, _ := new(dao.TaskDao).FindMulti(page, r)
	for _, item := range ls {
		m := models.TaskMysqlModel{ID: item.ID, Spec: item.Spec}
		task.Service.Run(&m)
	}
	return &response.APIResponse{Code: error_code.Success, Message: error_code.MsSuccess, Data: ""}
}

func (s *RunTask) CheckData() bool {
	return len(s.Req.IDs) > 0
}
